﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Active.Core;

public partial class User_Controls_BillInfoControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ApplyAuthorizationRules();
        }
    }

    private void ApplyAuthorizationRules()
    {
        Bill obj = GetBill();
        this.phOperations.Visible = obj.CanConfirm() || obj.CanCancelConfirmed();
        if (this.phOperations.Visible)
        {
            this.btnSubmitBill.Visible = obj.CanConfirm();
            this.btnCancelConfirmBill.Visible = obj.CanCancelConfirmed();
        }
    }

    protected void btnSubmitBill_Click(object sender, EventArgs e)
    {
        GetBill().Confirm();
        Response.Redirect(Request.RawUrl, true);
    }

    protected void btnCancelConfirmBill_Click(object sender, EventArgs e)
    {
        GetBill().CancelConfirmed();
        Response.Redirect("~/Bill", true);
    }

    #region Business Methods

    protected Bill GetBill()
    {
        string idString = Request.QueryString["id"];
        object businessObj = Session["CurrentObj"];
        if (businessObj == null || !(businessObj is Bill)
            || (!IsPostBack && !((Bill)businessObj).Id.ToString().Equals(idString)))
        {
            try
            {
                if (!string.IsNullOrEmpty(idString))
                {
                    businessObj = Bill.GetBill(int.Parse(idString));
                }
                else
                {
                    businessObj = Bill.NewBill();
                }
                Session["CurrentObj"] = businessObj;
            }
            catch (System.Security.SecurityException)
            {
                Response.Redirect("~/Bill");
            }
        }
        return businessObj as Bill;
    }

    #endregion

    protected void BillDataSource_SelectObject(object sender, Csla.Web.SelectObjectArgs e)
    {
        e.BusinessObject = GetBill();
    }
}
